<?php
date_default_timezone_set('Asia/Shanghai');

set_time_limit(0);

require_once('Snoopy.class.php');
require_once('Database.class.php');
require_once('Config.php');

function get_page($url , $encode = 'utf8' , $command = 'snoopy') {
	$page = false;

	if($command == 'snoopy') {
		$snoopy = new Snoopy;
		$snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
		$snoopy->referer = "http://www.google.de/";
		$snoopy->fetch($url);
		
		$page = $snoopy->results;
	} else if($command == 'wget') {
		@unlink('wget.result');
		$command = 'wget\wget.exe ' . $url . " -O wget.result";
		echo $command . "\n";
		system($command);
		if (file_exists('wget.result')) {
			$page = file_get_contents('wget.result');
		}
	}
	
	if ($page && $encode != "utf8") {
		$page = mb_convert_encoding($page, "utf8", $encode);
	}
	
	return $page;
}

function post_page($url , $data , $params = array() ) {
	$snoopy = new Snoopy;
	$snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
	$snoopy->referer = "http://www.google.de/";
	
	foreach($params as $key => $value) {
		$snoopy->$key = $value;
	}
	
	$snoopy->submit($url , $data);
	return $snoopy->results;
}

function strip_selected_tags($text , $tags = array())
{
	$args = func_get_args();
	$text = array_shift($args);
	$tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
	foreach ($tags as $tag){
		$text = preg_replace('/<'.$tag.'.*?>(.*?)<\/'. $tag .'>/is' , "\${1}" , $text);
	}

	return $text;
}

function remove_selected_tags($text , $tags = array())
{
	$args = func_get_args();
	$text = array_shift($args);
	$tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
	foreach ($tags as $tag){
		$text = preg_replace('/<'.$tag.'.*?>.*?<\/'. $tag .'>/is' , "" , $text);
	}

	return $text;
}